$(document).ready(function(){
    var bFocus = true, bCate = true;
    var eFocus = $('#focusBtn'), eFocusCount = $('#focusCount'), eCollectMsg = $('#collectMsg'), eAboutBtn = $('#aboutBtn'), eCateBtn = $('#cateBtn'), eAboutZone = $('#aboutZone'), eCateZone = $('#cateZone'),eCateDiv = $('#cateDiv'), eCateMsg = $('#cateMsg');
    // 收藏与取消收藏功能实现
    eFocus.on('click', function(e){
        e.preventDefault();
        if(!bFocus){
            clearpop('正在操作中，请稍后 ...');
            return ;
        }
        bFocus = false;
        var mark = $(this).data('mark');
        if(mark == 1){
            eCollectMsg.text('正在收藏该文档，请稍后...');
            $.post("/doc/ajaxfocus.html", {did : did, type : 1}, function(res){
                var data = JSON.parse(res);
                if(data.code == 200){
                    eFocusCount.text(collects + 1);
                    eFocus.data('mark', 2).removeClass('btn-default').addClass('btn-ed').text('取消收藏');
                    eCollectMsg.text('您已成功收藏该文档！');
                    bFocus = true;
                }else if(data.code == 400){
                    eCollectMsg.text(data.message).addClass('red');
                }else{
                    eCollectMsg.text('收藏系统出现异常，请刷新后再试！').addClass('red');
                }
            });
        }else if(mark == 2){
            eCollectMsg.text('正在取消收藏该文档，请稍后...');
            $.post("/doc/ajaxfocus.html", {did : did, type : 2}, function(res){
                var data = JSON.parse(res);
                if(data.code == 200){
                    eFocusCount.text(collects - 1);
                    eFocus.data('mark', 1).removeClass('btn-ed').addClass('btn-default').text('收藏「$1」');
                    eCollectMsg.text('您已成功取消收藏该文档！');
                    bFocus = true;
                }else if(data.code == 400){
                    eCollectMsg.text(data.message).addClass('red');
                }else{
                    eCollectMsg.text('收藏系统出现异常，请刷新后再试！').addClass('red');
                }
            });
        }else{
            eCollectMsg.text('收藏系统出现异常，请刷新后再试！').addClass('red');
        }
    });
    eAboutBtn.on('click', function(e){
        e.preventDefault();
        eCateZone.addClass('hide');
        eAboutZone.removeClass('hide');
        eCateBtn.removeClass('active');
        $(this).addClass('active');
    });
    eCateBtn.on('click', function(e){
        e.preventDefault();
        eAboutZone.addClass('hide');
        eCateZone.removeClass('hide');
        eAboutBtn.removeClass('active');
        $(this).addClass('active');
        if(bCate){
            // 获取目录内容
            bCate = false;
            setTimeout(function(){
                $.post('/doc/ajaxcate.html', {did : did}, function(res){
                    var data = JSON.parse(res);
                    if(data.code == 400){
                        alert('获取文档目录失败，请刷新后重试！');
                    }else if(data.code == 401){
                        // 没有目录
                        eCateMsg.text('文档暂未编辑「目录」');
                    }else if(data.code == 200){
                        eCateDiv.append('<div class="cateContainer">'+getDocCate(JSON.parse(data.data.content), did)+'</div>');
                        setDocCount('.cateContainer>ul>li');
                        eCateDiv.after('<div class="cover-time"><span>更新于 '+data.data.time+'</span></div>');
                        eCateMsg.remove();
                    }
                });
            }, 1000);
        }
    });

    // 实现分享按钮功能
    var shareUrl = 'http://www.jiathis.com/send/?webid=';
    var shareStr = '&url=http://doc.wooo.wang/doc/'+did+'.html&title='+docName+' - '+docDesc+' - 我网[wooo.wang]，分享与收获知识、经验、见解的团体协作书写平台！';
    $('#shareQQ').attr('href', shareUrl+'qzone'+shareStr);
    $('#shareWeibo').attr('href', shareUrl+'tsina'+shareStr);
    $('#shareWeixin').attr('href', shareUrl+'weixin'+shareStr);

    var eDonationBtn = $('#donationBtn'), eModal = $('#modal'), eModalBtn = $('#modalOk'), eGxInput = $('#gxInput');
    eDonationBtn.on('click', function(e){
        e.preventDefault();
        eModal.removeClass('hide').addClass('modal-show');
        window.setTimeout(function(){
            $('.modal').addClass('in');
        },20);
        eGxInput.focus();
    });
    eGxInput.blur(function(){
        var gx = eGxInput.val();
        var maxGx = eGxInput.data('gx');
        if(gx > 0 && gx <= maxGx) $(this).removeClass('error');
    });
    // 关闭modal
    eModal.on('click', '#modalClose,#modalNo', function(){
        eModal.removeClass('modal-show').removeClass('in');
        window.setTimeout(function(){
            eModal.addClass('hide');
        }, 1000);
    });
    eModalBtn.on('click', function(e){
        e.preventDefault();
        var gx = eGxInput.val();
        var maxGx = eGxInput.data('gx');
        if(gx <= 0 || gx > maxGx){
            eGxInput.addClass('error');
            return ;
        }
        $.post('/doc/donation.html', {did: did, gx: gx}, function(res){
            var data = $.parseJSON(res);
            if(data.code == 200){
                var newGx = maxGx - gx;
                eGxInput.data('gx', newGx).val('');
                $('#userAllGx').text(newGx);
                $('#modalClose').click();
            }
            clearpop(data.msg);
        });
    });
});